#include "../../global/QmlNames.h"
#include "../MessagesManager.h"
#include "MessageListModel.h"

X_BEGIN_NAMESPACE

enum RoleNames{
    id,
    content,
    thumbnail,
    senderJid,
    senderName,
    senderPhoto,
    time,
    contentType,
    DATA_ROLE_MAX
};

const char* MessageListModel::ROLE_NAMES[] = {
    "id",
    "content",
    "thumbnail",
    "senderJid",
    "senderName",
    "senderPhoto",
    "time",
    "contentType"
};

MessageListModel::MessageListModel(QObject *parent)
    : QAbstractListModel(parent)
    , d(new MessageListModelPrivate)
{
    for(int i = id; i < DATA_ROLE_MAX; ++i){
        m_roleNames.insert(i, ROLE_NAMES[i]);
    }
}

MessageListModel::MessageListModel(const MessageListModel &src)
    : QAbstractListModel(src.parent())
    , d(src.d)
{
    m_roleNames = src.roleNames();
}

MessageListModel &MessageListModel::operator =(const MessageListModel &rhs)
{
    if(this != &rhs) this->d = rhs.d;

    return *this;
}

int MessageListModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    return d->m_messages.size();
}

QVariant MessageListModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid()) return QVariant();
    if(index.row() >= d->m_messages.size()) return QVariant();

    MessageModel message = d->m_messages[index.row()];
    switch (role) {
        case id:
            return message.id();
        case content:
            return message.content();
        case thumbnail:
            return message.thumbnail();
        case senderJid:
            return message.senderJid();
        case senderName:
            return message.senderName();
        case senderPhoto:
            return message.senderPhoto();
        case time:
            return message.time();
        case contentType:
            return (int)message.contentType();
        default:
            return QVariant("Not Implement.");
    }
}

QHash<int, QByteArray> MessageListModel::roleNames() const
{
    return m_roleNames;
}

const char *MessageListModel::qmlName()
{
    return QmlNames::SubModelType::MessageListModel;
}

/**
 * @brief MessageListModel::insertMessage
 * @param messageId  为0时，生成一个新的本地消息ID
 * @param senderJid
 * @param senderName
 * @param senderPhoto
 * @param content
 * @param thumbnail
 * @param contentType
 * @param time
 * @param appendAtFront
 * @return
 */
MessageModel MessageListModel::insertMessage(int messageId,
                                             const QString &senderJid,
                                             const QString &senderName,
                                             const QString &senderPhoto,
                                             const QString &content,
                                             const QString &thumbnail,
                                             MessageContentType contentType,
                                             QDateTime time,
                                             bool appendAtFront)
{
    // 为0需要创建一个新的消息ID
    messageId = messageId == 0 ? MessagesManager::instance().nextMessageId() : messageId;

    MessageModel message(messageId);
    message.setContent(content)
            .setContentType(contentType)
            .setSenderJid(senderJid)
            .setSenderName(senderName)
            .setSenderPhoto(senderPhoto)
            .setTime(time)
            .setThumbnail(thumbnail);

    // 插入列表
    appendAtFront? d->m_messages.prepend(message) : d->m_messages.append(message);

    // 新插入数据，会发送rowsInserted信号，而不是dataChanged信号
    int first = appendAtFront ? 0 : d->m_messages.size() - 1;
    int last = appendAtFront ? 0 : d->m_messages.size() - 1;
    beginInsertRows(QModelIndex(), first, last);
    endInsertRows();

    return message;
}

X_END_NAMESPACE
